package beusoft.ccjk.com.calculator;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Created by CCJK on 2018/11/15.
 */

public class FileOperate {
    private String dir = "F:\\temp\\HarryPotter\\HarryPotter1";
    private File fileDir;
    private String subDirName = "chinese";
    private String name = "";
    private String seperate = "\n\n\n";

    public FileOperate(String subDirName, String name, String seperate) {
        this.subDirName = subDirName;
        this.name = name;
        this.seperate = seperate;
    }

    public static void main(String... args) {
        new FileOperate("english", "english.txt", "\r\n\r\n")
                .tojson();
//                .start();
    }


    private void tojson() {
        String filePath = "F:\\temp\\words.txt";
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(filePath));
            String line = null;
            String[] arr = null;
            ArrayList<Word> arrayList = new ArrayList<>();
            Word word;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.length() > 0) {
                    arr = line.split("                  q               ");
                    String english = arr[0].trim();
                    String chinese = arr[1].trim();
                    word = new Word();
                    word.english = english;
                    word.chinese = chinese;
                    arrayList.add(word);

                }
            }
            String jsonFilePath = "F:\\temp\\words.json";
            FileWriter writer = null;
            try {
                writer = new FileWriter(jsonFilePath);
                StringBuilder builder = new StringBuilder("[");
                for (Word word1 : arrayList) {
                    builder.append("{")
                            .append("\"is_new\"").append(":").append(word1.is_new).append(",")
                            .append("\"english\"").append(":\"").append(word1.english).append("\",")
                            .append("\"chinese\"").append(":\"").append(word1.chinese).append("\"")
                            .append("},");

                }
                builder.append("]");

                writer.write(builder.toString());
                writer.flush();
            } catch (Exception e) {

            } finally {
                if (writer != null)
                    writer.close();
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception e1) {
            }
        }


    }

    private void start() {
        fileDir = new File(dir);
        handleFile(new File(fileDir, name));
    }


    /**
     * \r\n\r\n\r\n
     *
     * @param file
     */
    private void handleFile(File file) {
        try {
            InputStream inputStream = new FileInputStream(file);
            byte[] arr = new byte[2048];
            int len;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            while ((len = inputStream.read(arr, 0, arr.length)) != -1) {
                outputStream.write(arr, 0, len);
            }
            String s = outputStream.toString();
            String[] list = s.split(seperate);
            String content;
            String name;
            File dir = new File(fileDir, subDirName);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            for (int i = 0; i < list.length; i++) {
                content = list[i].trim();
                name = i + ".txt";
                makeFile(content, name, dir);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void makeFile(String content, String name, File dir) throws Exception {
        FileWriter writer = new FileWriter(new File(dir, name));
        writer.write(content);
        writer.flush();
        writer.close();
    }


}
